//
//  HomeViewController.swift
//  LCYWeiBo
//
//  Created by 李长友 on 16/8/30.
//  Copyright © 2016年 app. All rights reserved.
//

import UIKit
import SVProgressHUD

class HomeViewController: BaseViewController {
    
    lazy var dataArray: [StatusModel] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitorView.setupAnimation()
        
        if !isLogin {
            return
        }
        
        setupNavigationBar()
        
        setupTableView()
        
        loadHomeStatuses()
    }
}

// MARK: - 设置UI
extension HomeViewController {
    
    //设置导航栏
    private func setupNavigationBar() {
        
        view.backgroundColor = UIColor.init(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1.0)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIButton(imageName: "navigationbar_friendattention"))
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: UIButton(imageName: "navigationbar_pop"))
        
        let titleButton = LeftTitleButton(imageName: "navigationbar_arrow_down", selectedImageName: "navigationbar_arrow_up")
        titleButton.setTitle(UserViewModel.shareInstance.user?.screen_name, forState: .Normal)
        titleButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
        titleButton.addTarget(self, action: #selector(HomeViewController.titleButtonClick(_:)), forControlEvents: .TouchUpInside)
        navigationItem.titleView = titleButton
    }
    
    //设置tableView
    private func setupTableView() {
        
        tableView.registerNib(UINib(nibName: "HomeStatusCell", bundle: nil), forCellReuseIdentifier: "homeStatus")
        
        tableView.separatorStyle = .None
        tableView.rowHeight = UITableViewAutomaticDimension
//        tableView.rowHeight = 500
        tableView.estimatedRowHeight = 200
    }

}

// MARK: - 按钮点击事件
extension HomeViewController {
    
    @objc private func titleButtonClick(titleBtn: LeftTitleButton) {
        
        titleBtn.selected = !titleBtn.selected
    }
}

// MARK: - 请求数据
extension HomeViewController {
    func loadHomeStatuses() {
        
        SVProgressHUD.show()
        
        NetworkTools.shareInstance.loadHomeStatuses { response in
            
            SVProgressHUD.dismiss()
            
            guard response.result.isSuccess else {
                debugLog(response.result.error)
                return
            }
            
            debugLog(response.result.value)
            
            guard let statuses = response.result.value?["statuses"] as? [[String : AnyObject]] else {
                return
            }
            
            debugLog(statuses)
            
            for status in statuses {
                let statusModel = StatusModel()
                statusModel.setValueWithDictionary(status)
                self.dataArray.append(statusModel)
            }
            
            self.tableView.reloadData()
        }
    }
}

// MARK: - Table view data source
extension HomeViewController {
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("homeStatus") as! HomeStatusCell
        
        cell.selectedBackgroundView = UIView()
        cell.selectedBackgroundView?.backgroundColor = UIColor(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1.0)
        
        cell.status = dataArray[indexPath.row]
        
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }
}










